package com.zsguan.z.meixi;

import android.app.Activity;
import android.app.ActivityManager;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.zsguan.z.slogger4andr.ZLogger;

/**
 * Created by chshqiang on 15-9-13.
 * Floating Activity
 */
public class ZFloatingActivity extends Activity {
    WindowManager mWindowManager;
    WindowManager.LayoutParams wmParams;
    LinearLayout mFloatLayout;
    Button mFloatView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_floating);

        Button start = (Button) findViewById(R.id.start);
        Button stop = (Button) findViewById(R.id.stop);

        start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                createFloatView();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mFloatLayout != null) {
                    mWindowManager.removeView(mFloatLayout);
//                    finish();
                }
            }
        });

        createFloatView();
    }

    private void createFloatView() {
        // 获取LayoutParams对象
        wmParams = new WindowManager.LayoutParams();

        // 获取的是LocalWindowManager对象
        mWindowManager = this.getWindowManager();
        // mWindowManager = getWindow().getWindowManager();
        ZLogger.i("mWindowManager2--->" + getWindow().getWindowManager());

        // 获取的是CompatModeWrapper对象
//         mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        wmParams.format = PixelFormat.RGBA_8888;

        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.x = 200;
        wmParams.y = 500;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        LayoutInflater inflater = this.getLayoutInflater();

        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);
        mWindowManager.addView(mFloatLayout, wmParams);
        mFloatView = (Button) mFloatLayout.findViewById(R.id.float_id);

        // 绑定触摸移动监听
        mFloatView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                wmParams.x = (int) event.getRawX() - mFloatLayout.getWidth() / 2;
                // 25为状态栏高度
                wmParams.y = (int) event.getRawY() - mFloatLayout.getHeight() / 2 - 40;
                mWindowManager.updateViewLayout(mFloatLayout, wmParams);
                return false;
            }
        });

        // 绑定点击监听
        mFloatView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                wmParams.x = 0;
                // 25为状态栏高度
                wmParams.y = 0;
                mWindowManager.updateViewLayout(mFloatLayout, wmParams);
            }
        });

        ActivityManager am = null;
        am.getRunningTasks(10);
    }


}
